<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Customer_model extends MY_Model {
	
	protected $customers;
	
	function __construct(){
		parent::__construct();
	}
	
	function table_name(){
		return "customers";
	}
	
	function insert_game($data){
		return $this->save($data);
	}
	
	
	
	function get_customers($filters = array(),$counting = false){
		if (isset($this->CI->cache)){
			$caching = true;
			$cache_key = 'ci--CUSTOMER_'.md5(serialize($filters));
			if ($counting){
				$cache_key = 'ci--CUSTOMER_COUNT_'.md5(serialize($filters));
			}
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		}else{
			$caching = false;
		}
		$this->db->select('customers.*')->from('customers');
		
		if (isset($filters['customer_id'])){
			$this->db->where('customers.customer_id',$filters['customer_id']);
		}
		
		if (isset($filters['username'])){
			$this->db->where('customers.username',$filters['username']);
		}
		
		if (isset($filters['limit'])) {
			$offset = (isset($filters['page'])) ? $filters['page'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		$this->db->order_by('customers.customer_id','DESC');
		$result = $this->db->get();
		
		if($counting !=false){
			$rows = $result->num_rows();
			$result->free_result();
			
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			
			return $rows;
		}
		
		if ($result->num_rows() == 0) {
			return false;
		}
		$customers = array();

		foreach ($result->result_array() as $row){
			$customers[] = $row;
		}
		
		if ($caching){
			$this->CI->cache->file->save($cache_key,$customers);
		}
		
		$result->free_result();
		return $customers;
	}
	
	function get_count($filters=array()){
		return $this->get_customers($filters,true);
	}
	
	function get_customer_by_id($customer_id){
		if (!isset($this->customers[$customer_id])){
			$customer = $this->get_customers(array('customer_id'=>$customer_id));
			$this->customers[$customer_id] = $customer;
		}else {
			$customer = $this->customers[$customer_id];
		}
		if (empty($customer)){
			return false;
		}
		return $customer[0];
	}
	
	function get_customer_by_username($username){
		
	}
	
	function login($username,$password){
		$result = $this->db->where('username',$username)
				->where('password',$this->has_password($password))
				->get('customers');
		if ($result->num_rows() > 0){
			$customer = $result->row_array();
			$this->set_customer_login($customer['customer_id']);
			return $customer;
		}
		return false;
	}
	
	function set_customer_login($customer_id){
		$this->session->set_userdata('customer',$customer_id);
		return $this;
	}
	
	function is_login(){
		return (bool)$this->session->userdata('customer') && (bool)$this->check_customer_id($this->session->userdata('customer'));
	}
	
	function check_customer_id($customer_id){
		$result = $this->db->where('customer_id',$customer_id)->limit(1,0)->get('customers')->num_rows();
		if ($result > 0) {
			return true;
		}
		return false;
	}

	function get_customer_login($param = null){
		if ($this->is_login()){
			$customer =  $this->get_customer_by_id($this->session->userdata('customer'));
			$customer = array_merge(array('vip'=>$this->customer_is_vip($customer)),$customer);
			if ($param == null){
				return $customer;
			}elseif (isset($customer[$param])){
				return $customer[$param];
			}
		}
		return false;
	}
	
	function customer_is_vip($customer){
		$publish_down_vip = fdate($customer['publish_down_vip'],'%Y-%m-%d');
		if (strtotime($publish_down_vip) > strtotime('now')){
			return khoangcachngay(fdate($publish_down_vip,'%d/%m/%Y'),fdate('now','%d/%m/%Y'));
		}
		return "0";
	}
	
	function is_vip(){
		if (($customer = $this->get_customer_login())){
			if ($customer['vip'] != "0" && $customer['vip'] > 0){
				return true;
			}
		}
		return false;
	}
	
	function validate_username($username){
		$result = $this->db->where('username',$username)->get('customers')->num_rows();
		if ($result >=1) {
			return true;
		}
		return false;
	}
	
	function logout(){
		$this->set_customer_login(null);
		$this->session->unset_userdata('customer');
		return true;
	}
	
	function gen_password($length = 5){
		$this->load->helper('string');
		$makepass = random_string('numeric',$length);
		return $makepass;
	}
	
	function has_password($password){
		return md5($password.setting('encryption_key'));
	}
	
	function create_customer($username,$password,$count_day_vip=''){
		if ($count_day_vip == '' || $count_day_vip == null){
			$publish_down_vip = fdate('now');
		}else{
			$publish_down_vip = toWeek('',false,$count_day_vip);
		}
		$attributes = array(
			'username'=>$username,
			'password'=>$this->has_password($password),
			'status'=>1,
			'created'=>fdate('now'),
			'publish_down_vip'=>$publish_down_vip
		);
		return $this->save($attributes);
	}
	
	function update_customer(){
		
	}
	
	function add_day_vip($username,$count_day_vip){
		$customer = $this->db->where('username',$username)->limit(1,0)->get('customers')->row();
		$publish_down_vip = toWeek($customer->publish_down_vip,false,$count_day_vip);
		return $this->update(array('publish_down_vip'=>$publish_down_vip),array('username'=>$username));
	}
	
	function delete_customer($customer_id){
		return $this->delete(array('customer_id'=>$customer_id));
	}
	
	function change_password($username,$password){
		return $this->db->update(array('password'=>$this->has_password($password)),array('username'=>$username));
	}
}